const http = require('http');
const net  = require('net');
const util = require('util');
var fs = require('fs')
//随便定义的一个函数，下面会用到
function  a(){
    console.log("dajiahao");
}
var server = new http.Server();
server.on('request',(req,res)=>{
    console.log(req.url);
    //设置应答头信息
    fs.readFile(req.url.substr(1),function(err,data){
        if(err){

        }
        else{
            res.writeHead(200,{'Content-Type':'text/html'});
            res.write(data.toString());
            res.end('server already end\n');
        }
    })
});
//显示了三次这也证明了TCP的三次握手
server.on('connection',()=>{
    a();
});
server.on('close',()=>{
    console.log('server will close');
});
//关闭服务为了触发close事件
server.close();
server.listen(8081);